<template>
  <!-- 首页 -->
  <div class="home">
    <!-- <van-button type="primary">主要按钮</van-button>
    <van-stepper v-model="value" />

    <van-cell-group>
      <van-cell title="单元格" value="内容" />
      <van-cell title="单元格" value="内容" label="描述信息" />
    </van-cell-group> -->

    <!--面试题: 页面出现弹框的时候,弹框后看见了弹框底层的页面内容,怎么解决 -->
    <transition name="van-fade">
      <!-- 给首页的内容加一个div套在外面 v-show 通过路径的值来决定是否展示.为了避免弹框底层的页面内容立刻消失,在div的外面再加一个transition name="van-fade"  用来解决立刻消失的问题 -->
      <div v-if="$route.path === '/home'">
        <!-- 首页顶部搜索栏 -->
        <van-search
          shape="round"
          placeholder="请输入搜索关键词"
          disabled
          @click="searchClick"
        />
        <!-- 首页banner轮播图 -->
        <van-swipe class="my-swipe" :autoplay="3000" indicator-color="#1989fa">
          <van-swipe-item v-for="item in banner" :key="item.id">
            <img :src="item.image_url" alt="" width="100%" />
          </van-swipe-item>
        </van-swipe>

        <!-- 首页频道 -->
        <Grid :channelArr="channelArr" />

        <Brand :brandList="brandList" />

        <NewGoods :newGoodsList="newGoodsList" />

        <!-- 首页专题精选 -->
        <Topic :topicList="topicList" />
      </div>
    </transition>
    <!-- 黑色遮罩层 -->
    <transition name="van-fade">
      <div class="popup-shadow" v-show="isShowShadow"></div>
    </transition>

    <!-- 展示搜索弹出框 -->

    <!-- vue封装好的写法 -->
    <!-- <transition name="van-slide-right"> -->
    <transition name="slide">
      <router-view></router-view>
    </transition>
  </div>
</template>

<script>
// @ is an alias to /src
// import HelloWorld from '@/components/HelloWorld.vue'

import { mapState, mapMutations } from "vuex";

import { GetHomeData } from "@/request/api";

import Grid from "../components/home/Grid.vue";
import Topic from "../components/home/Topic.vue";
import Brand from "../components/home/Brand.vue";
import NewGoods from "../components/home/NewGoods.vue";
export default {
  name: "Home",
  data() {
    return {
      banner: [],
      // 频道数据
      channelArr: [],
      // 专题数据
      topicList: [],
      brandList: [],
      newGoodsList: [],
    };
  },
  computed: {
    ...mapState({
      isShowShadow: (state) => state.showshadow.isShowShadow,
    }),
  },

  async created() {
    //async + await 处理异步函数嵌套异步函数的终极解决方案
    // 经验： 一般 await 后面放的是 promise对象。
    // this.chanIsShowShadow(false)
    let res = await GetHomeData();

    // then的res接受的是响应拦截器中返回的值
    // console.log(res.data.banner);
    // this.bannar = res.data.babnar
    // res.data
    // console.log(res.data.brandList);

    let { banner, channel, topicList, brandList, newGoodsList } = res.data;
    this.banner = banner;
    this.channelArr = channel;
    this.topicList = topicList;
    this.brandList = brandList;
    this.newGoodsList = newGoodsList;

    // 第一个请求成功之后，才会发起第二个请求
    // let res2 = await XXX();
    // res2
  },
  methods: {
    ...mapMutations({
      chanIsShowShadow: "showshadow/chanIsShowShadow",
    }),
    searchClick() {
      this.$router.push("/home/searchpopup");
      //修改vuex中isShowShadow的值,本质上是要调用vuex中的chanIsShowShadow这个方法
      this.chanIsShowShadow(true);
    },
  },
  components: {
    Grid,
    Topic,
    Brand,
    NewGoods,
  },
  // 路由监听
  // 不是路由守卫,只是用来监听父子级关系的路由的切换
  // watch: {
  //   $route(to, from) {
  //     // console.log("to为:", to);
  //     // console.log("from为:", from);
  //     if (to.path === "/home") {
  //       this.chanIsShowShadow(false);
  //     }
  //   },
  // },
};
</script>

<style lang="less" scoped>
// vue 的过渡效果的写法:
// (注意事项,在过渡盒子身上不要书写这个需要过度的属性)

// 进入过渡的初始状态,以及离开过渡的结束状态
.slide-enter,
.slide-leave-to {
  right: -100%;
}

// 过渡时间的设置
.slide-enter-active,
.slide-leave-active {
  transition: all 0.5s;
}

// 进入过渡的结束状态,以及离开过渡的初始状态
.slide-enter-to,
.slide-leave {
  right: 0;
}
.home {
  padding-bottom: 0.5rem;
}
.popup-shadow {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background-color: rgba(0, 0, 0, 0.6);
  z-index: 99;
}
</style>
